var propertyStrName;
function sortby(property){
    var propertyStr = $(property).html().trim();
    if(propertyStrName == $(property).html().replace(/<.[^<^>]*>/g,"").trim()){
        if(propertyStr.contains("caret")){
            if(propertyStr.contains("dropup")){
                propertyStr = propertyStr.replace(/<.[^<^>]*>/g,"");
                $(property).html(propertyStr);
                $("#orderBy").val("");
                $("#orderByname").val("");
            }else{
                propertyStr = propertyStr.replace(/<.[^<^>]*>/g,"");
                $(property).html(propertyStr + "<div class='btn-group dropup'><span class='caret'></span></div>");
                $("#orderBy").val("asc");
                $("#orderByname").val(propertyStr.replace(/<.[^<^>]*>/g,""));

            }
        }else{
            $(property).append("<span class='caret'></span>");
            $("#orderBy").val("desc");
            $("#orderByname").val(propertyStr.replace(/<.[^<^>]*>/g,""));
        }

    }else{
        var ths = $("th[id^='sort_']");
        for(var i=0;i<ths.length;i++){
            $(ths[i]).html($(ths[i]).html().replace(/<.[^<^>]*>/g,"").trim());
        }
        propertyStrName = $(property).html().replace(/<.[^<^>]*>/g,"").trim();
        $(property).append("<span class='caret'></span>");
        $("#orderBy").val("desc");
        $("#orderByname").val(propertyStr.replace(/<.[^<^>]*>/g,""));
    }
    topage('1');
}

function slideThePanel(panelId) {
    $("#testsPanel" + panelId).slideToggle("fast");
}

function topage(page) {
    var form = document.getElementById("myform");
    document.getElementById("page")
        .setAttribute("value", page);
    form.submit();
}

function sortArrow(){
    var ths = $("th[id^='sort_']");
    var orderbyname = $("#orderByname").val().trim();
    var orederby = $("#orderBy").val().trim();
    var thsName;

    if(orderbyname != ""){
        for(var i=0;i<ths.length;i++){
            thsName = $(ths[i]).html().replace(/<.[^<^>]*>/g,"").trim();
            if(thsName==orderbyname){
                propertyStrName = thsName;
                if(orederby=="asc"){
                    $(ths[i]).html(thsName+"<div class='btn-group dropup'><span class='caret'></span></div>");
                }else if(orederby=="desc"){
                    $(ths[i]).html(thsName+"<span class='caret'></span>");
                }
                break;
            }
        }
    }

}

function toggleSearchbutton(){
    $(".searchName").toggle();
    $(".tag-search").toggle();
}




function findSubject(subject){
    var tag_span = '<span class="tag"><a href="javascript:void(0);" tag="0">不限</a></span>';

    for (var i = 0; i < subject.length; i++) {
        tag_span =tag_span+
            '<span class="tag"><a href="javascript:void(0);" tag="'+subject[i].id+'">'+subject[i].subjectName+'</a></span>';

    }
    $('.tag_subject').append(tag_span);
    $('.tag_subject>.tag>a').click(function(){
        searchTag("searchSubject",$(this).attr("tag"));
        topage('1');
    });
    searchTagChooce();

}
function searchTag(searchName, tagId) {
    $("#" + searchName).val(tagId);
}


function searchTagChooce(){

    var category = $("#searchCategory").val();
    var subject = $("#searchSubject").val();
    var subjectTags = $(".tag_subject:hidden");
    for(var j=0;j<subjectTags.length;j++){
        var sublength = $(subjectTags[j]).find("a").length;
        for(var i=0;i<sublength;i++){
            var subjectTag= $($(subjectTags[j]).find("a")[i]).attr("tag");
            if(subjectTag==subject){
                $($(subjectTags[j]).find("span")[i]).attr("class","tag active");
                break;
            }
        }
    }

    var categoryTags = $(".tag_category:hidden");
    for(j=0;j<categoryTags.length;j++){
        var catlength = $(categoryTags[j]).find("a").length;
        for(i=0;i<catlength;i++){
            var categoryTag= $($(categoryTags[j]).find("a")[i]).attr("tag");
            if(categoryTag==category){
                $($(categoryTags[j]).find("span")[i]).attr("class","tag active");
                break;
            }
        }
    }

    var active = $(".active:hidden");
    var addSpan;
    var breadcrumb = $(".breadcrumb .title");
    if(active.length>2){
        var addClass;

        if(subjectTag==0 && categoryTag==0){
            breadcrumb.append('<span class="name">未选择</span>');
        }else if(subjectTag!=0 && categoryTag!=0){
            for(i=0;i<2;i++){
                if($($(active[i]).find("a")[0]).attr("tag")!=0){
                    if(i==0){
                        addClass = 'class="breadcrumb_subject"';
                    }else{
                        addClass = 'class="breadcrumb_category"';
                    }
                    addSpan = '<span class="name">'+$($(active[i]).find("a")[0]).html()
                        +'<a href="javascript:void(0);" '+addClass+'>x</a></span>';
                    breadcrumb.append(addSpan);
                }
                if(i<1){
                    breadcrumb.append('<span class="divider"> / </span>');
                }
            }
        }else{
            for(i=0;i<2;i++){
                if($($(active[i]).find("a")[0]).attr("tag")!=0){
                    if(i==0){
                        addClass = 'class="breadcrumb_subject"';
                    }else{
                        addClass = 'class="breadcrumb_category"';
                    }
                    addSpan = '<span class="name">'+$($(active[i]).find("a")[0]).html()
                        +'<a href="javascript:void(0);" '+addClass+'>x</a></span>';
                    breadcrumb.append(addSpan);
                }
            }
        }

    }

    $('.breadcrumb_subject').click(function(){
        searchTag("searchSubject","0");
        topage('1');
    });
    $('.breadcrumb_category').click(function(){
        searchTag("searchCategory","0");
        topage('1');
    });

}

